<?php

/*
 * 产品管理
 */

class GoodsAction extends Action {

    //产品列表
    public function goodslist() {
        $o_goods = M("goods");
		import("ORG.Util.Page");
        $count = $o_goods->count();
        $listrow = 15;
        $page = new Page($count, $listrow);
        $show = $page->show();
        $goods_list = $o_goods->order('goods_id')->limit($page->firstRow . "," . $page->listRows)->select();
		$this->assign('lists', $goods_list);
        $this->show = $show;
        $this->display();
    }

	//删除产品
	public function del($id){
		$del_goods = M('goods');
		if($del_goods->where('goods_id="'.$id.'"')->delete()){
			$this->success('删除成功',U('Goods/goodslist'));
		}else{
			$this->error('删除失败',U('Goods/goodslist'));
		}
	}
	//编辑产品
	public function edit() {
		if (IS_GET) {
			$data = I('get.');
			$o_goods = M("goods");
			$good = $o_goods->where('goods_id="' . $data['goods_id'] . '"')->find();
			$this->assign('goods',$good);
			$this->display();
		}
		if (IS_POST) {
			$datas = I('post.');
			$edit_goods = M("goods");
			//var_dump($datas);die();
			 if ($edit_goods->where('goods_id="' . $datas['goods_id'] . '"')->save($datas)) {
                $this->success('修改成功', U('Goods/goodslist'));
            } else {
                $this->error('修改失败', U('Goods/goodslist'));
            }
		}
	}
	//添加产品
	public function add() {
        if (IS_GET) {
            $this->display();
        }
        if (IS_POST) {
			import('ORG.Net.UploadFile');
			$upload = new UploadFile();// 实例化上传类
			$upload->maxSize  = 3145728 ;// 设置附件g上传大小
			$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->savePath  = './Public/Uploads/'; // 设置附件上传目录
			 if(!$upload->upload()) {// 上传错误提示错误信息
			$this->error($upload->getErrorMsg());
			 }else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
			 }
			 // 保存表单数据 包括附件数据
			$img0 = $info[0]['savename'];
			$img1 = $info[1]['savename'];
			$img2 = $info[2]['savename'];
			$img3 = $info[3]['savename'];
			$img4 = $info[4]['savename'];
			$img5 = $info[5]['savename'];
			$img6 = $info[6]['savename'];
			
            $data= I('post.');
			$data['goods_thumb'] = $img;
			$data['img1'] = $img1; 
			$data['img2'] = $img2; 
			$data['img3'] = $img3; 
			$data['img4'] = $img4;  
			$data['img5'] = $img5; 
			$data['img6'] = $img6; 
			$add_goods = M("goods");
            if ($add_goods->add($data)) {
                $this->success('添加成功', U('Goods/goodslist'));
            } else {
                $this->error('添加失败', U('Goods/add'));
            }
        }
    }


}
